//
//  StatusCellTopView.swift
//  新浪微博01
//
//  Created by 李国伟 on 15/12/11.
//  Copyright © 2015年 李国伟. All rights reserved.
//

import UIKit
class StatusCellTopView: UIView {

    var viewModel: StatusViewModel?{
        
        didSet{
            // 姓名
            nameLabel.text = viewModel?.status.user?.screen_name
            
            // 头像
            iconView.sd_setImageWithURL(viewModel?.profileURL,
                placeholderImage: viewModel?.userDefaultIcon)
            // 会员图标
            memberIconView.image = viewModel?.membership
            // 认证图标
            vipIconView.image = viewModel?.verified
            
            // TODO - 后续会讲
            // 时间
            timeLabel.text = "刚刚" // viewModle?.status.created_at
            sourceLabel.text = "来自 黑马微博" // viewModle?.status.source
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 懒加载控件
    /// 头像
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    /// 姓名
    private lazy var nameLabel: UILabel = UILabel(title: "王老五", fontsize: 14)
    /// 会员图标
    private lazy var memberIconView: UIImageView = UIImageView(image: UIImage(named:"common_icon_membership_level1"))
    /// 认证图标
    private lazy var vipIconView: UIImageView = UIImageView(image: UIImage(named:"avatar_vip"))
    /// 时间标签
    private lazy var timeLabel: UILabel = UILabel(title: "现在", fontsize: 11, color: UIColor.orangeColor())
    /// 来源标签
    private lazy var sourceLabel: UILabel = UILabel(title: "来源", fontsize: 11)
    
}


//添加控件设置frame信息
extension StatusCellTopView{
    
    private func setupUI (){
    
        let separatorView = UIView()
        separatorView.backgroundColor = UIColor(white: 0.8, alpha: 1)
        
        // 1. 添加控件
        addSubview(separatorView)
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(memberIconView)
        addSubview(vipIconView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        
        // 2. 自动布局
        separatorView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self)
            make.left.equalTo(self)
            make.right.equalTo(self)
            make.height.equalTo(StatusCellMargin)
        }
        iconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(separatorView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(self.snp_left).offset(StatusCellMargin)
            make.width.equalTo(StatusCellIconWidth)
            make.height.equalTo(StatusCellIconWidth)
        }
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_top)
            make.left.equalTo(iconView.snp_right).offset(StatusCellMargin)
        }
        memberIconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(nameLabel.snp_top)
            make.left.equalTo(nameLabel.snp_right).offset(StatusCellMargin)
        }
        vipIconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_right)
            make.centerY.equalTo(iconView.snp_bottom)
        }
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView.snp_bottom)
            make.left.equalTo(iconView.snp_right).offset(StatusCellMargin)
        }
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(timeLabel.snp_bottom)
            make.left.equalTo(timeLabel.snp_right).offset(StatusCellMargin)
        }
    }
}